package com.dushibao.service.impl;

import com.dushibao.common.Constants;
import com.dushibao.dao.UserDao;
import com.dushibao.dao.UserMenuDao;
import com.dushibao.entity.User;
import com.dushibao.service.UserService;
import com.dushibao.utils.Page;

import java.util.List;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDao();

    UserMenuDao userMenuDao = new UserMenuDao();

    @Override
    public Integer login(String userName, String password) {
        List<User> users = userDao.queryByUserNameAndPassword(userName,password);

        if(users == null || users.size() == 0){
            return Constants.USER_NOT_FOUND;
        }
        if(users.size()==1){
            return Constants.SUCCESS;
        }
        return Constants.USERNAME_PASSWORD_ERROR;
    }

    @Override
    public Page page(Page page, String userName) {
        return userDao.page(page,userName);
    }

    @Override
    public User getById(long id) {
        return userDao.getById(id);
    }


    @Override
    public Integer saveOrUpdate(User user,List<Long> ids) {
        if(user.getId()!=null){

            userMenuDao.delete(user.getId());

            userMenuDao.save(user.getId(),ids);

            return userDao.update(user);
        }
        userDao.save(user);

        List<User> users = userDao.queryByUserName(user.getUserName());

        userMenuDao.save(users.get(0).getId(),ids);

        return Constants.SUCCESS;
    }

    @Override
    public Integer deleteById(List<Long> ids) {
        return userDao.deleteById(ids);
    }

    @Override
    public Integer save(User user) {
        List<User> users = userDao.queryByUserName(user.getUserName());
        if(users.size()>0){
            return Constants.ERROR;
        }
        return userDao.save(user);
    }

    @Override
    public List<User> queryByUserNameAndPassword(String userName, String password) {
        return userDao.queryByUserNameAndPassword(userName,password);
    }

    @Override
    public Integer update(User user) {
        return userDao.update(user);
    }

    @Override
    public List<User> list(List<Long> userIds) {
        return userDao.list(userIds);
    }

    @Override
    public boolean saveBatch(List<User> users) {

        return userDao.saveBatch(users);
    }
}
